package devbureau.fstore.common.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;

public class DeleteOrderLineAction {

  	private static Logger logger = Logger.getLogger(CreateOrderLineAction.class);

    public static final String DELETE_ORDER_LINE = "delete from STORE.ORDER_LINE where ID = ?";
    public static final String UPDATE_ITEM_BALANCE = "update STORE.ITEM set BALANCE = BALANCE +  ?  where ID = ?";

    
    public DeleteOrderLineAction() {
    }

    protected void  execute(Connection connection,Integer lineId,Integer itemId,Integer quantity)throws Exception{
        logger.info("#execute enter");
        logger.info("#execute lineId : " + lineId);
        logger.info("#execute itemId : " + itemId);
        logger.info("#execute quantity : " + quantity);

        PreparedStatement ps = null;
        ps = connection.prepareStatement(UPDATE_ITEM_BALANCE);
        ps.setInt(1, quantity);
        ps.setInt(2, itemId);

        ps.executeUpdate();
        ps.close();

        logger.info("#run update OK");

        // insert order items
         ps = connection.prepareStatement(DELETE_ORDER_LINE);

         ps.setInt(1, lineId);
         ps.executeUpdate();
         ps.close();
    }

}
